package com.atguigu.es.demo;

import com.atguigu.es.beans.Emp;
import com.atguigu.es.beans.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.*;

import java.io.IOException;

/**
 * Created by Smexy on 2021/7/9
 *
 *     写操作
 *          Bulk
 *
 *

 */
public class WriteDemo3 {

    public static void main(String[] args) throws IOException {

        //创建一个 JestClientFactory
        JestClientFactory jestClientFactory = new JestClientFactory();

        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        // 从工厂中获取一个 客户端对象
        JestClient jestClient = jestClientFactory.getObject();

        Emp emp = new Emp("1031", "mike", "male", "吃饭", 30, 2222.22);


        // 执行插入
        Index index = new Index.Builder(emp)
                .type("emps")
                .index("test")
                .id("24")
                .build();


        //执行删除 22号员工
        Delete delete = new Delete.Builder("22").index("test").type("emps").build();

        String str="{\n" +
                "  \"id\":5,\n" +
                "  \"name\":\"《翠花洗酸菜》\"\n" +
                "}";


        Movie movie = new Movie(30, "《翠花洗酸菜2》");

        // 执行向  movie_index 插入
        Index index3 = new Index.Builder(str)
                .type("movie_type")
                .index("movie_index")
                .id("9")
                .build();

        // 执行向  movie_index 插入
        Index index2 = new Index.Builder(movie)
                .type("movie_type")
                .index("movie_index")
                .id("9")
                .build();


        Bulk bulk = new Bulk.Builder()
               // .addAction(index)
                .addAction(index2)
                //.addAction(delete)
                .build();

        BulkResult bulkResult = jestClient.execute(bulk);

        System.out.println(bulkResult.getItems().size());

        //关闭客户端
        jestClient.close();


    }
}
